#include <iostream>
#include <ctemplate/template.h>

#include "../comm/util.hpp"
#include "oj_model.hpp"

namespace ns_ojview
{
    using namespace ns_ojmodel;
    const std::string temp_path = "./template_html/";

    class View
    {
    public:
        //加载所有题目到列表中
        bool LoadHtml(const std::vector<Question>& qlist,std::string* html)
        {
            std::string src_path = temp_path + "all_questions.html"; 
            //1.形成数字典
            ctemplate::TemplateDictionary root("all_questions");
            for(const auto& q:qlist)
            {
                ctemplate::TemplateDictionary* tmp = root.AddSectionDictionary("questions_list");
                tmp->SetValue("number",q.number);
                tmp->SetValue("title",q.title);
                tmp->SetValue("level",q.level);
                //LOG(INFO) << "for" << "\n";
            }
            //2.渲染
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_path,ctemplate::DO_NOT_STRIP);
            tpl->Expand(html,&root);
            return true;
        }

        //加载一个题目到列表中
        bool LoadHtml(const Question& q,std::string* html)
        {
            std::string src_path = temp_path + "one_question.html";
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number",q.number);
            root.SetValue("title",q.title);
            root.SetValue("desc",q.desc);
            root.SetValue("level",q.level);
            root.SetValue("pre_code",q.header);

            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_path,ctemplate::DO_NOT_STRIP);
            tpl->Expand(html,&root);

            return true;
        }

    };
}
